//
//  SearchShopViewController.m
//  何五路
//
//  Created by _ADY on 15/7/3.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "SearchShopViewController.h"
#import "SearchViewController.h"

@interface SearchShopViewController ()

@end

#define menuWidth screenMySize.size.width/4
#define mHight (screenMySize.size.width*3/4-30)/3
@implementation SearchShopViewController
@synthesize menuTableView,mTableView,dic;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, menuWidth, screenMySize.size.height)];
    menuTableView.delegate = self;
    menuTableView.dataSource = self;
    menuTableView.backgroundColor = [UIColor colorWithRed:243/255.0 green:244/255.0 blue:246/255.0 alpha:1];
    [self.view addSubview:menuTableView];
    
    UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [menuTableView setTableFooterView:v1];
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(menuWidth, 64, screenMySize.size.width-menuWidth, screenMySize.size.height-64)];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];
    
    UISearchBar *m_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(100, 100, 200, 41)];
    m_searchBar.delegate = self;
    m_searchBar.backgroundColor = [UIColor clearColor];
    m_searchBar.placeholder = (@"搜索所有商品");
    m_searchBar.keyboardType =  UIKeyboardTypeDefault;
    [self.navigationItem setTitleView:m_searchBar];
    
    for (UIView *view in m_searchBar.subviews) {//去除背景色
        
        if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
            [[view.subviews objectAtIndex:0] removeFromSuperview];
            break;
        }
    }
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    SearchViewController *details = [[SearchViewController alloc] init];
    details.hidesBottomBarWhenPushed = YES;
    details.stringText = @"1";
    details.stringId = nil;
    [self.navigationController pushViewController:details animated:YES];
    
    searchBar.showsCancelButton = NO;
    return YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    xzShopInt = 0;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *ret2 = [SendService GetStoreCategories];
        dispatch_async(dispatch_get_main_queue(), ^{
             NSLog(@"商品分类接口 %@",ret2);
            dic = [ret2 objectForKey:@"Entity"];
            [menuTableView reloadData];
            [mTableView reloadData];
        });
    });
    
    
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == menuTableView) {
        return dic.count;
    }
    else if (tableView == mTableView)
       return [[[dic objectAtIndex:xzShopInt] objectForKey:@"SubCategories"] count];
    return YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (tableView == menuTableView)
       return 50;
    else if (tableView == mTableView)
    {
        int  m = ([[[[[dic objectAtIndex:xzShopInt] objectForKey:@"SubCategories"] objectAtIndex:indexPath.row] objectForKey:@"SubCategories"] count])/3;
        int  n = ([[[[[dic objectAtIndex:xzShopInt] objectForKey:@"SubCategories"] objectAtIndex:indexPath.row] objectForKey:@"SubCategories"] count])%3;
        if (n== 0)
            return m*(mHight+20)+30;
        else
            return (m+1)*(mHight+20)+30;
        
    }
    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"searchShop";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    if (aTableView == menuTableView)
    {
        aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
       return [self mCell:cell MneuCTableView:aTableView cellForRowAtIndexPath:indexPath];
    }
    else if (aTableView == mTableView)
    {
        aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        return [self mCell:cell bCTableView:aTableView cellForRowAtIndexPath:indexPath];
    }
    return cell;
}
- (UITableViewCell *)mCell:(UITableViewCell*)cell MneuCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor clearColor];
    if (xzShopInt  == indexPath.row)
    {
        cell.backgroundColor = [UIColor whiteColor];
    }
    [cell.contentView addSubview:[self setFrame:CGRectMake(0, 0, menuWidth, 50) setTitle:[NSString stringWithFormat:@"%@",[[dic objectAtIndex:indexPath.row] objectForKey:@"Name"]] setType:indexPath.row]];

    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell bCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"Sub1Categories %d  \n %@",[[[dic objectAtIndex:xzShopInt] objectForKey:@"SubCategories"] count],[[dic objectAtIndex:xzShopInt] objectForKey:@"SubCategories"]);
    NSMutableArray *nsArray = [[[[dic objectAtIndex:xzShopInt] objectForKey:@"SubCategories"] objectAtIndex:indexPath.row] objectForKey:@"SubCategories"];
    
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(10, 0, screenMySize.size.width-menuWidth-10, 30) setTitle:[NSString stringWithFormat:@"%@",[[[[dic objectAtIndex:xzShopInt] objectForKey:@"SubCategories"] objectAtIndex:indexPath.row] objectForKey:@"Name"]]]];
    
    
    for (int i = 0; i < [nsArray count]; i ++) {
        
        [cell.contentView addSubview:[self setTitle:[[nsArray objectAtIndex:i] objectForKey:@"Name"]  setImage:[[nsArray objectAtIndex:i] objectForKey:@"PictureUrl"] setTage:i setTag:indexPath.row*1000+i]];
    }

    return cell;
}

-(UIButton*)setTitle:(NSString *)title setImage:(NSString*)imageD setTage:(int)tyPage setTag:(int)theTag
{
//    imageD = @"http://172.16.36.241:8061/content/images/thumbs/0001374.png";
    
    UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake((tyPage%3)*(mHight+10)+10,30+(mHight+20)*(int)((tyPage-0)/3), mHight,mHight);
    [inButton setFrame:frame];
    inButton.tag = theTag;
    [inButton addTarget:self action:@selector(bInBction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, mHight, mHight)];
    [bgImageView sd_setImageWithURL:[NSURL URLWithString:imageD]];
    bgImageView.clipsToBounds  = YES;
    bgImageView.backgroundColor = [UIColor grayColor];
    bgImageView.layer.cornerRadius = 5.0f;
    [inButton addSubview:bgImageView];
    
    UILabel* label = [[UILabel alloc] init];
    [label setFrame:CGRectMake(0,mHight, mHight, 15)];
    label.textAlignment = 1;
    label.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.text = title;
    [inButton addSubview:label];
    
    return inButton;
}

-(void)bInBction:(id)sender
{
    int type = ((UIButton*)sender).tag;
    int ind = type%1000;
    int dsc = type/1000;
    NSMutableArray *nsArray = [[[[dic objectAtIndex:xzShopInt] objectForKey:@"SubCategories"] objectAtIndex:dsc] objectForKey:@"SubCategories"];
    
    SearchViewController *details = [[SearchViewController alloc] init];
    details.hidesBottomBarWhenPushed = YES;
    details.stringText = @"0";
    details.stringId = [[nsArray objectAtIndex:ind] objectForKey:@"Id"];
    [self.navigationController pushViewController:details animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (tableView == menuTableView) {
        xzShopInt  = indexPath.row;
        [menuTableView reloadData];
        [mTableView reloadData];
    }else if (tableView == mTableView)
    {
        
    }
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize];
    titleLabel.textColor = [UIColor grayColor];

    return titleLabel;
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setType:(int)type
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 1;
    titleLabel.numberOfLines = 0;
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize];
    
    titleLabel.textColor = [UIColor blackColor];
    if (xzShopInt  == type)
    {
        titleLabel.textColor = [UIColor redColor];
    }
    return titleLabel;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
